'use strict'

require('./index.css');
var _mm = require('util/mm.js');

var header = {
    init: function () {
        this.load();
        this.bindEvent();
    },
    load: function () {
        //如果有keyword，则回填输入框
        var keyword = _mm.getUrlParam('keyword');
        if (keyword) {
            $('.search-input').val(keyword);
        }
    },
    bindEvent: function () {
        var _this = this;
        //搜索按钮点击
        $('.search-btn').click(function () {
            _this.searchSubmit();
        });
        //回车事件
        $('.search-input').keyup(function (e) {
            if (e.keyCode === 13) {
                _this.searchSubmit();
            }
        });
    },
    //搜索的提交
    searchSubmit: function () {
        var keyword = $('#search-input').val();
        //如果keyword存在，正常跳转到list页
        if (keyword) {
            window.location.href = './list.html?keyword=' + keyword;
        }
        //如果不存在，跳到主页
        else {
            _mm.goHome();
        }
    }
};
header.init();
